package javolution.testing;

import cn.qqtheme.framework.adapter.FileAdapter;
import com.google.android.exoplayer2.C;
import javolution.context.Context;
import javolution.context.LogContext;
import javolution.context.ObjectFactory;
import javolution.lang.Configurable;
import javolution.lang.MathLib;
import javolution.text.TextBuilder;

/* loaded from: classes2.dex */
public abstract class TimeContext extends TestContext {
    private long _averagePs;
    private long _maximumPs;
    private long _minimumPs;
    private TestCase _testCase;
    public static final Class<TimeContext> REGRESSION = Regression.CLASS;
    public static final Configurable<Integer> TEST_DURATION_MS = new Configurable<>(new Integer(1000));
    public static final Configurable<Class<? extends TimeContext>> DEFAULT = new Configurable<>(Default.CLASS);

    /* loaded from: classes2.dex */
    private static final class Default extends TimeContext {
        private static final Class CLASS = new Default().getClass();

        private Default() {
        }

        private static TextBuilder appendTime(long j, TextBuilder textBuilder) {
            String str;
            long j2 = 1000;
            if (j > 1000000000000L) {
                str = " s";
                j2 = 1000000000000L;
            } else if (j > C.NANOS_PER_SECOND) {
                str = " ms";
                j2 = 1000000000;
            } else if (j > 1000000) {
                str = " us";
                j2 = 1000000;
            } else if (j > 1000) {
                str = " ns";
            } else {
                j2 = 1;
                str = " ps";
            }
            long j3 = j / j2;
            textBuilder.append(j3);
            int digitLength = 3 - MathLib.digitLength(j3);
            if (digitLength > 0) {
                textBuilder.append(FileAdapter.DIR_ROOT);
            }
            int i = 0;
            int i2 = 10;
            while (i < digitLength) {
                textBuilder.append(((i2 * j) / j2) % 10);
                i++;
                i2 *= 10;
            }
            return textBuilder.append(str);
        }

        @Override // javolution.testing.TimeContext, javolution.testing.TestContext
        public void doTest(TestCase testCase) {
            super.doTest(testCase);
            TextBuilder newInstance = TextBuilder.newInstance();
            newInstance.append("[test] ");
            newInstance.append(testCase.toString());
            newInstance.append(": ");
            appendTime(getAverageTimeInPicoSeconds(), newInstance);
            newInstance.append(" (minimum ");
            appendTime(getMinimumTimeInPicoSeconds(), newInstance);
            newInstance.append(")");
            newInstance.println();
            TextBuilder.recycle(newInstance);
        }

        @Override // javolution.context.LogContext
        public boolean isErrorLogged() {
            return true;
        }

        @Override // javolution.context.LogContext
        public boolean isInfoLogged() {
            return true;
        }

        @Override // javolution.context.LogContext
        public boolean isWarningLogged() {
            return true;
        }

        @Override // javolution.context.LogContext
        public void logError(Throwable th, CharSequence charSequence) {
            System.out.print("[error] ");
            if (th != null) {
                System.out.print(th.getClass().getName());
                System.out.print(" - ");
            }
            System.out.println(charSequence != null ? charSequence.toString() : th != null ? th.getMessage() : "");
            if (th != null) {
                th.printStackTrace();
            }
        }

        @Override // javolution.context.LogContext
        public void logInfo(CharSequence charSequence) {
            System.out.print("[info] ");
            System.out.println(charSequence);
        }

        @Override // javolution.context.LogContext
        public void logWarning(CharSequence charSequence) {
            System.out.print("[warning] ");
            System.out.println(charSequence);
        }
    }

    /* loaded from: classes2.dex */
    private static final class Regression extends TimeContext {
        private static final Class CLASS = new Regression().getClass();

        private Regression() {
        }

        @Override // javolution.context.LogContext
        public boolean isErrorLogged() {
            return false;
        }

        @Override // javolution.context.LogContext
        public boolean isInfoLogged() {
            return false;
        }

        @Override // javolution.context.LogContext
        public boolean isWarningLogged() {
            return false;
        }

        @Override // javolution.context.LogContext
        public void logError(Throwable th, CharSequence charSequence) {
        }

        @Override // javolution.context.LogContext
        public void logInfo(CharSequence charSequence) {
        }

        @Override // javolution.context.LogContext
        public void logWarning(CharSequence charSequence) {
        }
    }

    static {
        ObjectFactory.setInstance(new ObjectFactory() { // from class: javolution.testing.TimeContext.1
            /* JADX INFO: Access modifiers changed from: protected */
            @Override // javolution.context.ObjectFactory
            public Object create() {
                return new Default();
            }
        }, Default.CLASS);
        ObjectFactory.setInstance(new ObjectFactory() { // from class: javolution.testing.TimeContext.2
            /* JADX INFO: Access modifiers changed from: protected */
            @Override // javolution.context.ObjectFactory
            public Object create() {
                return new Regression();
            }
        }, Regression.CLASS);
    }

    public static TimeContext enter() {
        return (TimeContext) Context.enter((Class<Context>) DEFAULT.get());
    }

    public static TimeContext exit() {
        return (TimeContext) Context.exit();
    }

    public static long getAverageTime(String str) {
        LogContext current = LogContext.getCurrent();
        if (current instanceof TimeContext) {
            return picosecondTo(str, ((TimeContext) current).getAverageTimeInPicoSeconds());
        }
        return -1L;
    }

    public static long getMaximumTime(String str) {
        LogContext current = LogContext.getCurrent();
        if (current instanceof TimeContext) {
            return picosecondTo(str, ((TimeContext) current).getMaximumTimeInPicoSeconds());
        }
        return -1L;
    }

    public static long getMinimumTime(String str) {
        LogContext current = LogContext.getCurrent();
        if (current instanceof TimeContext) {
            return picosecondTo(str, ((TimeContext) current).getMinimumTimeInPicoSeconds());
        }
        return -1L;
    }

    private static long nanoTime() {
        return System.nanoTime();
    }

    private static long picosecondTo(String str, long j) {
        long j2;
        if (str.equals("ps")) {
            return j;
        }
        if (str.equals("ns")) {
            j2 = 1000;
        } else if (str.equals("us")) {
            j2 = 1000000;
        } else if (str.equals("ms")) {
            j2 = C.NANOS_PER_SECOND;
        } else if (!str.equals("s")) {
            throw new IllegalArgumentException("Unit " + str + " not recognized");
        } else {
            j2 = 1000000000000L;
        }
        return j / j2;
    }

    @Override // javolution.testing.TestContext
    public boolean doAssertEquals(String str, Object obj, Object obj2) {
        if ((obj != null || obj2 == null) && (obj == null || obj.equals(obj2))) {
            return true;
        }
        LogContext.error(this._testCase.toString());
        throw new AssertionException(str, obj, obj2);
    }

    @Override // javolution.testing.TestContext
    public void doTest(TestCase testCase) {
        this._testCase = testCase;
        System.gc();
        try {
            Thread.sleep(200L);
        } catch (InterruptedException unused) {
        }
        this._minimumPs = Long.MAX_VALUE;
        long j = 0;
        this._maximumPs = 0L;
        this._averagePs = 0L;
        long intValue = TEST_DURATION_MS.get().intValue() * C.NANOS_PER_SECOND;
        long j2 = 0;
        while (true) {
            testCase.prepare();
            try {
                long nanoTime = nanoTime();
                testCase.execute();
                long nanoTime2 = (nanoTime() - nanoTime) * 1000;
                long count = testCase.count();
                j += count;
                j2 += nanoTime2;
                long j3 = nanoTime2 / count;
                if (j3 < this._minimumPs) {
                    this._minimumPs = j3;
                }
                if (j3 > this._maximumPs) {
                    this._maximumPs = j3;
                }
                if (j2 >= intValue) {
                    this._averagePs = j2 / j;
                    testCase.validate();
                    return;
                }
                testCase.cleanup();
            } finally {
                testCase.cleanup();
            }
        }
    }

    public long getAverageTimeInPicoSeconds() {
        return this._averagePs;
    }

    public long getMaximumTimeInPicoSeconds() {
        return this._maximumPs;
    }

    public long getMinimumTimeInPicoSeconds() {
        return this._minimumPs;
    }
}
